<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Common\Repositories\System;

use App\Common\Dao\System\CustomerPermissionsDao;
use App\Common\Repositories\BaseRepository;
use App\Exception\AppException;
use Hyperf\DbConnection\Db;
use xsl\enums\Code;
use xsl\enums\Status;

/**
 * Class BaseRepository.
 * @mixin CustomerPermissionsRepository
 */
class CustomerPermissionsRepository extends BaseRepository
{
    protected bool $checkPassword = true;

    public function __construct(CustomerPermissionsDao $dao)
    {
        $this->dao = $dao;
    }

    public function getList(array $where, $with=[]){
        $query = $this->dao->search($where);
        if($with){
            $query->with($with);
        }
        $data = $query->orderBy("id","desc")->get();
        $list = $this->toTree($data, 0, 'parent_id', 'children');
        return $list;
    }

    public function toggleBy($id, string $field = 'status'){
        $model = $this->dao->get($id);
        $status = $model->$field == Status::ENABLE ? Status::DISABLE : Status::ENABLE;
        Db::beginTransaction();
        try{
            $update = [$field => $status];
            $this->dao->update(intval($id), $update);
            $this->updateChildren($id, $field, $status);
            Db::commit();
        } catch(\Throwable $ex){
            Db::rollBack();
            throw new AppException($ex->getMessage(), Code::ERROR);
        }
        return true;
    }

    public function updateChildren(mixed $parentId, string $field, mixed $value){
        if(!is_array($parentId)){
            $parentId = [$parentId];
        }
        $childrenId = $this->dao->getInColumn("parent_id", $parentId , 'id');
        if (count($childrenId)) {
            if ($this->dao->updates($childrenId, [$field => $value])) {
                $this->updateChildren($childrenId, $field, $value);
            }
        }
    }

    public function storeBy(array $params){
        if(isset($params['actions']) && $params['actions']){
            $info = $this->dao->getWhere(['id'=>$params['parent_id']]);

            $temp[] = [
                'permission_name' => '列表',
                'parent_id' => $params['parent_id'],
                'module' => $info['module'],
                'permission_mark' => $info['permission_mark']. "@" ."index",
                'type' => 3,
                'creator_id' => $params['creator_id'],
            ];
            $temp[] = [
                'permission_name' => '新增',
                'parent_id' => $params['parent_id'],
                'module' => $info['module'],
                'permission_mark' => $info['permission_mark']. "@" ."create",
                'type' => 3,
                'creator_id' => $params['creator_id'],
            ];
            $temp[] = [
                'permission_name' => '更新',
                'parent_id' => $params['parent_id'],
                'module' => $info['module'],
                'permission_mark' => $info['permission_mark']. "@" ."update",
                'type' => 3,
                'creator_id' => $params['creator_id'],
            ];

            $temp[] = [
                'permission_name' => '删除',
                'parent_id' => $params['parent_id'],
                'module' => $info['module'],
                'permission_mark' => $info['permission_mark']. "@" ."delete",
                'type' => 3,
                'creator_id' => $params['creator_id'],
            ];

            $temp[] = [
                'permission_name' => '读取',
                'parent_id' => $params['parent_id'],
                'module' => $info['module'],
                'permission_mark' => $info['permission_mark']. "@" ."read",
                'type' => 3,
                'creator_id' => $params['creator_id'],
            ];
            $this->dao->insertAll($temp);
            return true;
        }
        $this->dao->create($params);
        return true;
    }
}
